package thread;

public class SyncDemo {
    public static void main(String[] args) {
        Shop shop = new Shop();
        Thread t1 = new Thread(){
            @Override
            public void run() {
                shop.buy();
            }
        };
        Thread t2 = new Thread(shop::buy);//方法引用,被重写的方法和当前对象调用的方法参数列表一致，则可以简化为方法引用格式 当前对象：：方法
        t1.start();
        t2.start();
    }


    static class Shop {
        public void buy(){
            try {
                Thread t = Thread.currentThread();
                System.out.println(t.getName()+"挑衣服。");
                Thread.sleep(3000);
                synchronized (this) {
                    System.out.println(t.getName() + "试衣服");
                    Thread.sleep(3000);
                }
                System.out.println(t.getName()+"结账离开。");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
